/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the EternalTech source code.

 The EternalTech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The EternalTech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the EternalTech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// sv_commands.cpp - operator console commands
//

// NOTE:
// - The save/loadgame will be a bit different then the original way. What i have in mind is something
// similar to Diablo 1 Multiplayer / Borderlands where the game it self is somewhat always open to
// the internet/LAN for others to join, the only way you can really be alone in a game is if you set a variable
// to singleplayer. The term "single player" it it's essence will not really be there because you are always "online".
//
// So we basically just save a server to a file and load it when desired, this will make it available to others if they
// want to join.
//
// - Add more commands if needed


#include "sv_local.h"


/*
 ==================
 
 ==================
*/
static void SV_WipeSaveGame (const char *saveName){

}

/*
 ==================
 
 ==================
*/
static void SV_CopySaveGame (const char *src, const char *dst){

}

/*
 ==================
 
 ==================
*/
static void SV_WriteServerFile (bool autoSave){

}

/*
 ==================
 
 ==================
*/
static void SV_ReadServerFile (){

}

/*
 ==================
 
 ==================
*/
void SV_WriteLevelFile (){

}

/*
 ==================
 
 ==================
*/
void SV_ReadLevelFile (){

}


// ============================================================================


/*
 ==================
 SV_Map_f
 ==================
*/
void SV_Map_f (){

	char	name[MAX_PATH_LENGTH];

	if (Cmd_Argc() != 2){
		Com_Printf("Usage: map <name>\n");
		return;
	}

	Str_Copy(name, Cmd_Argv(1), sizeof(name));
	Str_StripFileExtension(name);

	// Spawn the server
	SV_SpawnServer(name, false);
}

/*
 ==================
 SV_DevMap_f
 ==================
*/
void SV_DevMap_f (){

	char	name[MAX_PATH_LENGTH];

	if (Cmd_Argc() != 2){
		Com_Printf("Usage: devMap <name>\n");
		return;
	}

	Str_Copy(name, Cmd_Argv(1), sizeof(name));
	Str_StripFileExtension(name);

	// Spawn the server
	SV_SpawnServer(name, true);
}

/*
 ==================
 
 ==================
*/
void SV_LoadGame_f (){

	char		name[MAX_PATH_LENGTH];
	const char	*directory;

	if (Cmd_Argc() != 2){
		Com_Printf("Usage: loadGame <directory>\n");
		return;
	}

	directory = Cmd_Argv(1);
	if (Str_FindText(directory, "..", false) || Str_FindText(directory, "/", false) || Str_FindText(directory, "\\", false)){
		Com_Printf("Bad save directory\n");
		return;
	}

	if (!Str_ICompare(directory, "current")){
		Com_Printf("Can't load from 'current'\n");
		return;
	}

	// Make sure the server.ssv file exists
	Str_SPrintf(name, sizeof(name), "save/%s/server.ssv", directory);
	if (!FS_FileExists(name)){
		Com_Printf("No such savegame '%s'\n", name);
		return;
	}

	// Load the map
	Com_Printf("Loading game...\n");

	SV_CopySaveGame(directory, "current");

	SV_ReadServerFile();
}

/*
 ==================
 
 ==================
*/
void SV_SaveGame_f (){

	char		name[MAX_PATH_LENGTH];
	const char	*directory;

	if (Cmd_Argc() != 2){
		Com_Printf("Usage: saveGame <directory>\n");
		return;
	}

	directory = Cmd_Argv(1);
	if (Str_FindText(directory, "..", false) || Str_FindText(directory, "/", false) || Str_FindText(directory, "\\", false)){
		Com_Printf("Bad save directory\n");
		return;
	}

	if (!Str_ICompare(directory, "current")){
		Com_Printf("Can't load from 'current'\n");
		return;
	}

	// Write the level file
	Com_Printf("Saving game...\n");
}